from eth_utils.crypto import keccak
from eth_account import Account
from protocal.did import DID
from protocal.diddocument import (DIDDocument, DIDDocVerificationMethod, DIDDocService)
from protocal.didcodec import DIDCodec

doc = DIDDocument()
doc.id = "abcdefg"
doc.alsoKnownAs = "abc"
doc.controller = "controller"
doc.alsoKnownAs = "kent"
doc.service = DIDDocService()
doc.service.id = "sid"
doc.service.serviceEndpoint = "http://localhost:8822/s"


if False:
    print("JSON->", DIDCodec.to_json(doc))
    print("JSON-LD-->", DIDCodec.to_jsonld(doc))
    print("CBOR-->", DIDCodec.to_cbor(doc))


jstr = '''{
    "@Context": [
        "https://www.w3.org/ns/did/v1",
        "https://w3id.org/security/suites/ed25519-2020/v1"
    ],
    "id": "did:example:123456789abcdefghi",
    "controller": ["did:example:test:bcehfew7h32f32h7af3","did:example:test:7777788"],
    "alsoKnownAs": ["https://myblog.blogging-host.example/home"],
    "verificationMethod": [
        {
            "id": "did:example:123#_Qq0UL2Fq651Q0Fjd6TvnYE-faHiOpRlPVQcY_-tA4A",
            "controller": ["did:example:123","did:example:123"],
            "type": "JsonWebKey2020",
            "publicKeyJwk": {
                "crv": "Ed25519",
                "x": "VCpo2LMLhn6iWku8MKvSLg2ZAoC-nlOyPVQaO3FxVeQ",
                "kty": "OKP",
                "kid": "_Qq0UL2Fq651Q0Fjd6TvnYE-faHiOpRlPVQcY_-tA4A"
            }
        },
        {
            "id": "did:example:123456789abcdefghi#keys-1",
            "controller": "did:example:pqrstuvwxyz0987654321",
            "type": "Ed25519VerificationKey2020",
            "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV"
        }
    ],
    "authentication": [
        "did:example:123456789abcdefghi#keys-1",
        {
          "id": "did:example:123456789abcdefghi#keys-2",
          "type": "Ed25519VerificationKey2020",
          "controller": "did:example:123456789abcdefghi",
          "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV"
        }
    ],
    "assertionMethod": [
        "did:example:123456789abcdefghi#keys-1",
        {
            "id": "did:example:123456789abcdefghi#keys-2",
            "type": "Ed25519VerificationKey2020",
            "controller": "did:example:123456789abcdefghi",
            "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV"
        }
    ],
    "keyAgreement": [
        "did:example:123456789abcdefghi#keys-1",
        {
            "id": "did:example:123#zC9ByQ8aJs8vrNXyDhPHHNNMSHPcaSgNpjjsBYpMMjsTdS",
            "type": "X25519KeyAgreementKey2019",
            "controller": "did:example:123",
            "publicKeyMultibase": "z9hFgmPVfmBZwRvFEyniQDBkz9LmV7gDEqytWyGZLmDXE"
        }
    ],
    "capabilityInvocation": [
        "did:example:123456789abcdefghi#keys-1",
        {
            "id": "did:example:123456789abcdefghi#keys-2",
            "type": "Ed25519VerificationKey2020",
            "controller": "did:example:123456789abcdefghi",
            "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV"
        }
    ],
    "capabilityDelegation": [
        "did:example:123456789abcdefghi#keys-1",
        {
            "id": "did:example:123456789abcdefghi#keys-2",
            "type": "Ed25519VerificationKey2020",
            "controller": "did:example:123456789abcdefghi",
            "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV"
        }
    ],
    "service": [{
    "id":"did:example:123#linked-domain",
    "type": "LinkedDomains",
    "serviceEndpoint": ["https://bar.example.com","https://bar1.example.com"]
  }]
}'''
# print(json.loads(jstr))
doc = DIDCodec.from_json(jstr, DIDDocument)
print("DOC-->", doc)
print("JSON-LD-->", DIDCodec.to_jsonld(doc))
cbordata = DIDCodec.to_cbor(doc)
print(f"CBORDATA-->{len(cbordata)}:{cbordata}")
doc = DIDCodec.from_cbor(cbordata, DIDDocument)
print(DIDCodec.to_jsonld(doc))
strres = DIDCodec.to_json(doc, indent=None)
print(f"JSON-UNSORT: {len(strres)}:{strres}")
strres = DIDCodec.to_json(doc, indent=None, do_sort=True)  # test sort
print(f"JSON-DOSORT: {len(strres)}:{strres}")

cid = DID.loads(doc.controller[0])
print(cid.detail())
key = "7a94d9793bcc38f533c6e15d8ef9c557e8ead2d3f86e9ac1178ce56b2815f86b"
# key = "6eb7d91a86ee378defd30ef8abb9b72499845c05846a2d408583f0e1a4903f5d"
ethAccount: Account = Account.from_key(key)
strres = DIDCodec.to_json(doc, indent=None, do_sort=True)  # test sort
signres = ethAccount.signHash(keccak(bytes(strres, "UTF-8")))
print("SIGNRES ->", signres)
